package com.ml.mall.api.analytics;

import com.ml.mall.annotations.RequiresRoutings;
import com.ml.mall.api.BaseController;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.req.admin.variant.VariantQueryOneReq;
import com.ml.mall.bean.req.purchase.PurchaseCategoryQueryPageReq;
import com.ml.mall.bean.req.purchase.PurchaseSupplierQueryPageReq;
import com.ml.mall.bean.req.purchase.PurchaseVariantQueryPageReq;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.service.AnalyticsPurchaseService;
import com.ml.mall.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by kyyang on 2021/6/8 21:39
 */
@Api(tags = "【analytics】买家")
@RestController
@RequestMapping("/purchase")
@Slf4j
public class AnalyticsPurchaseController extends BaseController {



    @Autowired
    private AnalyticsPurchaseService analyticsPurchaseService;


    @RequestMapping(method = RequestMethod.POST, path = "/overview/query_report")
    @ApiOperation("买家查询首页消费报告")
//    @RequiresPermissions(PermissionConst.PURCHASE_OVERVIEW_QUERY_REPORT)
//  @RequiresRoutings(PermissionConst.PURCHASE_OVERVIEW_QUERY_REPORT)
    public ApiRestResult purchaseOverview(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPurchaseService.getPurchaseOverview(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/spent/query_report")
    @ApiOperation("买家查询消费报告")
//    @RequiresPermissions(PermissionConst.PURCHASE_SPENT_QUERY_REPORT)
//  @RequiresRoutings(PermissionConst.PURCHASE_SPENT_QUERY_REPORT)
    public ApiRestResult purchaseSpentQueryReport(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPurchaseService.getPurchaseSpentQueryReport(req));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/supplier/query_report")
    @ApiOperation("查询消费前10的供应商；没有全部0；缺少补0")
//    @RequiresPermissions(PermissionConst.PURCHASE_SUPPLIER_QUERY_REPORT)
//  @RequiresRoutings(PermissionConst.PURCHASE_SUPPLIER_QUERY_REPORT)
    public ApiRestResult purchaseSupplierQueryReport(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPurchaseService.getPurchaseSupplierQueryReport(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/supplier/query_page")
    @ApiOperation("消费订单的卖家 分页查询")
//    @RequiresPermissions(PermissionConst.PURCHASE_SUPPLIER_QUERY_PAGE)
//  @RequiresRoutings(PermissionConst.PURCHASE_SUPPLIER_QUERY_PAGE)
    public ApiRestResult purchaseSupplierQueryPage(@Valid @RequestBody PurchaseSupplierQueryPageReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPurchaseService.getPurchaseSupplierQueryPage(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/category/query_page")
    @ApiOperation("消费目录汇总分页查询")
    public ApiRestResult purchaseCategoryQueryPage(@Valid @RequestBody PurchaseCategoryQueryPageReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPurchaseService.purchaseCategoryQueryPage(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/variant/query_page")
    @ApiOperation("分页查询买家消费产品")
//    @RequiresPermissions(PermissionConst.PURCHASE_VARIANT_QUERY_PAGE)
//  @RequiresRoutings(PermissionConst.PURCHASE_VARIANT_QUERY_PAGE)
    public ApiRestResult purchaseVariantQueryPage(@Valid @RequestBody PurchaseVariantQueryPageReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPurchaseService.purchaseVariantQueryPage(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/variant/query_top")
    @ApiOperation("查询买家商品变体销售报告topN")
//    @RequiresPermissions(PermissionConst.PURCHASE_VARIANT_QUERY_TOP)
//  @RequiresRoutings(PermissionConst.PURCHASE_VARIANT_QUERY_TOP)
    public ApiRestResult supplierVariantQueryTop(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPurchaseService.purchaseVariantQueryTop(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/variant/query_one")
    @ApiOperation("买家查询某一个变体的时间范围内的销售报告")
//    @RequiresPermissions(PermissionConst.PURCHASE_VARIANT_QUERY_ONE)
//  @RequiresRoutings(PermissionConst.PURCHASE_VARIANT_QUERY_ONE)
    public ApiRestResult supplierVariantQueryOne(@Valid @RequestBody VariantQueryOneReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPurchaseService.purchaseVariantQueryOne(req));
    }


}
